package com.sojson.util.mq.rabbit.code;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.sojson.constant.Constant;
import com.sojson.util.text.Convert;

import lombok.extern.slf4j.Slf4j;

/**
 * Rabbit消息连接工具类
 * 
 * @author liu
 * @date 2021-07-11
 */
@Slf4j
public class RabbitChannelUtil {

    /** 代理服务器地址(集群模式) */
    public static final String ADDRESSES = Constant.PROPERTIES.getProperty("so.mq.rabbit.addresses");
    /** 代理服务器IP(单机模式) */
    public static final String HOST = Constant.PROPERTIES.getProperty("so.mq.rabbit.host");
    /** 代理服务器端口号(默认为5672)(单机模式) */
    public static final int PORT = Convert.toInt(Constant.PROPERTIES.getProperty("so.mq.rabbit.port"), 5672);
    /** 代理服务器帐号 */
    public static final String USERNAME =
        Convert.toStr(Constant.PROPERTIES.getProperty("so.mq.rabbit.username"), "root");
    /** 代理服务器密码 */
    public static final String PASSWORD =
        Convert.toStr(Constant.PROPERTIES.getProperty("so.mq.rabbit.password"), "123456");
    /** 连接断开后重新连接的间隔时间(单位: 毫秒,默认10秒) */
    public static final int TIME_RE_LOAD =
        Convert.toInt(Constant.PROPERTIES.getProperty("so.mq.rabbit.timeReLoad"), 1000 * 10);

    /**
     * 获取连接
     * 
     * @return
     */
    public static Channel getChannel() {
        try {
            // 获取连接工厂
            ConnectionFactory connectionFactory = getConnectionFactory();
            // 获取连接
            Connection connection = connectionFactory.newConnection();
            // 通过连接去创建传输数据的通道
            return connection.createChannel();
        } catch (Exception e) {
            throw new RuntimeException("获取Channel连接失败");
        }
    }

    /**
     * 获取连接工厂
     * 
     * @return
     */
    public static ConnectionFactory getConnectionFactory() {
        ConnectionFactory connectionFactory = new ConnectionFactory();

        // 配置连接信息
        connectionFactory.setHost(HOST);
        connectionFactory.setPort(PORT);
        connectionFactory.setUsername(USERNAME);
        connectionFactory.setPassword(PASSWORD);

        /// connectionFactory.setHost("192.168.100.101");
        /// connectionFactory.setPort(5672);
        /// connectionFactory.setUsername("root");
        /// connectionFactory.setPassword("123456");
        /// connectionFactory.setNetworkRecoveryInterval(1000 * 10);

        // 网络异常自动连接恢复
        connectionFactory.setAutomaticRecoveryEnabled(true);
        // 每N秒尝试重试连接一次
        connectionFactory.setNetworkRecoveryInterval(TIME_RE_LOAD);

        return connectionFactory;
    }

    /**
     * 关闭资源(一般消费者不关闭,一直监听队列消息并消费)
     * 
     * @param channel
     * @throws Exception
     */
    public static void close(Channel channel) throws Exception {
        if (channel != null) {
            try {
                channel.close();
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            }
        }
    }

    /**
     * 关闭资源(一般消费者不关闭,一直监听队列消息并消费)
     * 
     * @param connection
     * @throws Exception
     */
    public static void close(Connection connection) throws Exception {
        if (connection != null) {
            try {
                connection.close();
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            }
        }
    }

    /**
     * 关闭资源(一般消费者不关闭,一直监听队列消息并消费)
     * 
     * @param channel
     * @param connection
     * @throws Exception
     */
    public static void close(Channel channel, Connection connection) throws Exception {
        if (channel != null) {
            try {
                channel.close();
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            }
        }
        if (connection != null) {
            try {
                connection.close();
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            }
        }
    }

}